19. Exercise: Jackson

Exercise: Jackson

This time, you're going to try serializing UdacisearchClient to JSON (and then deserializing it from JSON), using the Jackson JSON library.

Just like the Java Object Serialization exercise, this is supposed to be just a straightforward write-then-read to make sure you can get the serialization/deserialization to work.

Read the TODO in Main.java and complete it:

  1. Create a com.fasterxml.jackson.databind.ObjectMapper instance and call ObjectMapper#registerModule() to register a com.fasterxml.jackson.datatype.jsr310.JavaTimeModule. This Jackson plugin is required to be able to serialize and deserialize the classes from the java.time package.
  2. Next, call ObjectMapper#writeValue(Files.newBufferedWriter(outputPath), client) to serialize to JSON.
  3. Similarly, call ObjectMapper#readValue(...) to read the newly created JSON file and deserialize the UdacisearchClient object.
  4. Add some System.out.printlns to prove that everything was serialized and deserialized as expected, then run the Main program:
javac Main.java
java Main client.json

All done!

TODO List

Task List:

Task Feedback:

Nice work! You converted a Java class to and from the JSON data format! Working with JSON is pretty easy!

Code

If you need a code on the https://github.com/udacity.

  • userCode:

    export PATH=/data/jdk-15.0.1/bin:$PATH
    export JAVA_HOME=/data/jdk-15.0.1/bin
    export CLASSPATH="/home/workspace:/home/workspace/lib/*"